package com.mfl.controller.admin;

import com.mfl.common.PageResult;
import com.mfl.common.Result;
import com.mfl.pojo.dto.CourseCategoryDTO;
import com.mfl.pojo.dto.CourseCategoryPageQueryDTO;
import com.mfl.pojo.vo.CourseCategoryVO;
import com.mfl.service.CourseCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController("adminCourseCategoryController")
@RequestMapping("/cky/admin/courseCategory")
@Slf4j
public class CourseCategoryController {

    @Autowired
    private CourseCategoryService courseCategoryService;

    /**
     * 添加课程分类
     *
     * @param courseCategoryDTO
     * @return
     */
    @RequestMapping("/insert")
    public Result insert(@RequestBody CourseCategoryDTO courseCategoryDTO) {
        log.info("添加课程分类：{}", courseCategoryDTO);
        courseCategoryService.insert(courseCategoryDTO);
        return Result.success("添加成功");
    }

    /**
     * 分页查询课程分类
     * @param courseCategoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(CourseCategoryPageQueryDTO courseCategoryPageQueryDTO) {
        log.info("分页查询课程分类：{}", courseCategoryPageQueryDTO);
        PageResult pageResult = courseCategoryService.pageQuery(courseCategoryPageQueryDTO);
        return Result.success("", pageResult);
    }

    /**
     * 批量删除课程分类
     * @param ids
     * @return
     */
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestParam List<Integer> ids){
        log.info("批量删除课程分类：{}", ids);
        courseCategoryService.batchDelete(ids);
        return Result.success("删除成功");
    }

    /**
     * 修改课程分类状态
     * @param status
     * @param requestBody
     * @return
     */
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, @RequestBody Map<String, Object> requestBody){
        // 直接用Integer id来接收参数始终为空，转为map接收
        Integer id = (Integer) requestBody.get("id");
        log.info("修改课程分类状态：id：{}, status：{}", id, status);
        courseCategoryService.updateStatus(status, id);
        return Result.success("修改成功");
    }

    /**
     * 根据课程分类id查询课程分类
     * @param id
     * @return
     */
    @GetMapping("/getByCourseCategoryId")
    public Result<CourseCategoryVO> getByCourseCategoryId(int id){
        log.info("根据课程分类id查询课程分类：{}", id);
        CourseCategoryVO courseCategoryVO = courseCategoryService.getByCourseCategoryId(id);
        return Result.success("", courseCategoryVO);
    }

    /**
     * 修改课程分类
     * @param courseCategoryDTO
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody CourseCategoryDTO courseCategoryDTO){
        log.info("修改课程分类：{}", courseCategoryDTO);
        courseCategoryService.update(courseCategoryDTO);
        return Result.success("修改成功");
    }

    /**
     * 查询所有启用的课程分类
     * @return
     */
    @GetMapping("/getAll")
    public Result<List<CourseCategoryVO>> getAll(){
        log.info("查询所有启用的课程分类");
        List<CourseCategoryVO> courseCategoryVOList = courseCategoryService.getAll();
        return Result.success("", courseCategoryVOList);
    }
}
